package it.cst.entity.service;


import it.cst.entity.Group;
import it.cst.entity.Match;
import it.cst.entity.Player;
import it.cst.entity.User;
import it.cst.entity.controller.MatchController;
import it.cst.entity.dao.UserDAO;
import it.cst.util.Roles;
import it.cst.util.SpringApplicationContext;
import it.cst.util.UserStat;
import it.cst.util.UserStatComparator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDAO userDAO;
	
	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Transactional
	public void addUser(User user) {
		userDAO.addUser(user);
	}

	@Transactional
	public List<User> listUser() {
		return userDAO.listUser();
	}

	@Transactional
	public void removeUser(Integer id) {
		userDAO.removeUser(id);
	}
	
	@Transactional
	public User getUser(String username) {
		return userDAO.getUser(username);
	}

	@Transactional
	public void saveUser(User user) {
		userDAO.saveUser(user);
	}

	@Transactional
	public User getUser(Integer userId) {
		return userDAO.getUser(userId);
	}
	
	
	@Transactional
	public List<UserStat> getUserStat() {
		List<UserStat> ret = new ArrayList<UserStat>();
		PlayerService ps = (PlayerService) SpringApplicationContext.getBean("playerService");
		GroupService gs = (GroupService) SpringApplicationContext.getBean("groupService");
		MatchService ms = (MatchService) SpringApplicationContext.getBean("matchService");
		UserService uss = (UserService) SpringApplicationContext.getBean("userService");
		HashMap<Integer,UserStat> hp = new HashMap<Integer,UserStat>();
		List<Player> pl = ps.listPlayer();
		if(pl != null && !pl.isEmpty()){
			for(Player p : pl){
				if(hp.get(p.getIduser()) == null){
					UserStat us = new UserStat();
					//us.setUser(p.getUser());
					us.setUser(uss.getUser(p.getIduser()));
					//Group g = p.getGroup();
					Group g = gs.getGroup(p.getIdgroup());
					//us.getMatches().add(g.getMatch());
					us.getMatches().add(ms.getMatch(g.getIdmatch()));
					if(g.getWinner().compareTo(new Integer(1)) == 0){
						us.setTotMatchWin(1);
					}
					else{
						us.setTotMatchLoose(1);
					}
					hp.put(p.getIduser(),us);
				}
				else{
					UserStat us = hp.get(p.getIduser());
					//Group g = p.getGroup();
					Group g = gs.getGroup(p.getIdgroup());
					//us.getMatches().add(g.getMatch());
					us.getMatches().add(ms.getMatch(g.getIdmatch()));
					if(g.getWinner().compareTo(new Integer(1)) == 0){
						us.setTotMatchWin(us.getTotMatchWin() + 1);
					}
					else{
						us.setTotMatchLoose(us.getTotMatchLoose() + 1);
					}
				}
			}
			for(Integer key : hp.keySet()){
				UserStat us = hp.get(key);
				us.setTotMatch(us.getTotMatchWin() + us.getTotMatchLoose());
				us.setPercMatchWin(new Double(us.getTotMatchWin()) / us.getTotMatch() * 100);
				us.setPercMatchLoose(new Double(us.getTotMatchLoose()) / us.getTotMatch() * 100);
				ret.add(hp.get(key));
			}
			//order by : perc win asc, perc loose desc, #match asc
			java.util.Collections.sort(ret, new UserStatComparator());
		}
		return ret;
	}

	@Transactional
	public HashMap<String, User> listLastMatchUser() {
		HashMap<String, User> ret = new HashMap<String, User>();
		MatchService ms = (MatchService) SpringApplicationContext.getBean("matchService");
		RoleService rs = (RoleService) SpringApplicationContext.getBean("roleService");
		UserService us = (UserService) SpringApplicationContext.getBean("userService");
		Roles roles = Roles.getInstance(rs);
		Match lastMatch = ms.getLastMatch();		
		if(lastMatch != null){
			   List<Group> gl = lastMatch.getGroups();
			   if(gl != null && !gl.isEmpty()){
				   for(Group g : gl){
					   List<Player> pl = g.getPlayers();
					   if(pl != null && !pl.isEmpty()){
						   for(Player p : pl){
							   //User u = p.getUser();
							   User u = us.getUser(p.getIduser());
							   if(g.getColor().equalsIgnoreCase(MatchController.RED_COLOR)){
								   if(p.getIdrole().compareTo(roles.getRole(Roles.DEFENSE_ROLE_NAME).getId()) == 0){
									   ret.put(MatchController.USER_RED_DEFENSE,u);
								   }
								   else{
									   ret.put(MatchController.USER_RED_ATTAC,u);
								   }
							   }
							   else{
								   if(p.getIdrole().compareTo(roles.getRole(Roles.DEFENSE_ROLE_NAME).getId()) == 0){
									   ret.put(MatchController.USER_BLU_DEFENSE,u);
								   }
								   else{
									   ret.put(MatchController.USER_BLU_ATTAC,u);
								   }
							   }
						   } 
					   }
				   }
			   }
		   }
		return ret;
	}
	
	
	
}
